#include "ConfigParser.h"
#include <fstream>
#include <iostream>
#include <filesystem>

bool ConfigParser::load(const std::string& filename) {
    std::ifstream file(filename);
    if (!file) {
        std::cerr << "无法打开配置文件: " << filename << std::endl;
        return false;
    }

    std::string line;
    while (std::getline(file, line)) {
        // 跳过空行或注释行
        if (line.empty() || line[0] == '#') continue;

        auto pos = line.find('=');
        if (pos != std::string::npos) {
            std::string key = line.substr(0, pos);
            std::string val = line.substr(pos + 1);
            config_[key] = val;
        }
    }
    return true;
}

std::string ConfigParser::get(const std::string& key) const {
    auto it = config_.find(key);
    return (it != config_.end()) ? it->second : "";
}

void ConfigParser::printAll() const {
    std::cout << ">>> 配置文件内容:\n";
    for (const auto& [key, val] : config_) {
        std::cout << key << " = " << val << "\n";
    }
}

bool ConfigParser::pathExists(const std::string& key) const {
    std::string path = get(key);
    if (path.empty()) return false;
    return std::filesystem::exists(path);
}
